// pages/interactionnew/interactionnew.js
const app = getApp()
const qiniu = require("../../utils/qiniu.min.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    activityProcess: "",
    posterUrl: [],
    dirty:[]
  },
  bindTextAreaBlur(e) {
    this.setData({
      activityProcess: e.detail.value,
    })
  },
  delete: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    console.log('删除', index)
    that.remove(that.data.posterUrl, that.data.posterUrl[index]);
    that.setData({
      posterUrl: that.data.posterUrl
    });
    console.log('删除', that.data.posterUrl)
  },
  //删除元素
  remove: function (array, val) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == val) {
        array.splice(i, 1);
      }
    }
  },
  save() {
    var that = this
    // console.log('评论内容', that.data.activityProcess, that.data.posterUrl.length)
    if (that.data.activityProcess == '' && that.data.posterUrl.length<=0){
      console.log('评论内容k', that.data.activityProcess)
      wx.showToast({
        title: '评论内容不能为空',
        icon:'none',
        duration: 2000
      })
      return
    }else{
      console.log('评论内容s', that.data.activityProcess)
      if (that.data.activityProcess != '') {
        var n = 0
        // console.log('词库2', this.data.dirty.length)
        for (var i = 0; i < this.data.dirty.length; i++) {
          // console.log('循环')
          if (that.data.activityProcess.indexOf(this.data.dirty[i]) >= 0) {
            console.log('查询2')
            wx.showToast({
              title: '您输入的内容有违禁词，敏感词已被替换成星号,请编辑后重新提交',
              icon: 'none',
              duration: 2000
            })
            var fw = this.data.dirty[i]
            var fw1 = ''
            for (var k = 0; k < fw.length; k++) {
              fw1 = fw1 + '*'
            }
            var activityProcess = that.data.activityProcess.replace(fw, fw1)
            that.setData({
              activityProcess: activityProcess
            });
            console.log('替换', that.data.activityProcess)
            // console.log('替换1', activityProcess)
            n = n + 1
          }
        }
        if (n > 0) {
          return
        }
      }
        app.req({
          url: 'wechat/actcomments/save',
          method: 'post',
          data: {
            activityId: that.data.id,
            content: that.data.activityProcess,
            picturesUrl: that.data.posterUrl
          },
          success: function (res) {
            if (res.code == 200) {
              wx.navigateBack({
                delta: 1
              })
              wx.showToast({
                title: '发布成功',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 2000
              })
            }
          }
        })
      
    }
  },
  //活动概况图片
  posterimg() {
    var that = this;
    if (that.data.posterUrl.length >= 3) {
      wx.showToast({
        title: '最多只能上传3张图片',
        icon: 'none',
        duration: 2000
      })
    } else {
      app.getQiNiuToken(function (res) {
        var countnum = 3 - that.data.posterUrl.length
        var qiniuconfig = res;
        wx.chooseImage({
          count: countnum,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success: function (res) {
            const filepathArray = res.tempFilePaths
            for (let i = 0; i < filepathArray.length; i++) {
              qiniu.upload(filepathArray[i], res => {
                that.setData({
                  posterUrl: that.data.posterUrl.concat([res.imageURL])
                })
                console.log("网络图片", that.data.posterUrl)
              }, (error) => {
                console.log('error' + error)
              }, {
                  uploadURL: 'https://upload.qiniup.com',
                  region: 'ECN',
                  domain: qiniuconfig.qiniuDomain,
                  uptoken: qiniuconfig.upToken,
                })
            }

          }
        })
      });
    }
  },
  //图片点击预览
  previewImg: function (e) {
    // console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    var imgArr = this.data.posterUrl;
    // console.log('图片地址', this.data.act2.posterUrl)
    // console.log(imgArr);
    wx.previewImage({
      current: imgArr[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  getdirty() {
    var that = this
    app.req({
      url: "wechat/dirty/all",
      method: "post",
      success: function (res) {
        // console.log('脏字库')
        if (res.code == 200) {
          wx.setStorageSync('dirty', res.data);
          that.setData({
            dirty: res.data
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    if (wx.getStorageSync('dirty') == undefined || wx.getStorageSync('dirty')=='') {
      this.getdirty()
      // console.log('无词库')
    } else {
      // console.log('有词库', wx.getStorageSync('dirty'))
      this.setData({
        dirty: wx.getStorageSync('dirty')
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})